<?php
    require_once ('scripts.php');
	require_once ('model/dao/vo/UserVO.php');
	$action = $_REQUEST['action'];	
	if($action == 'login')
	{ 	
		$object=new VehicleVO();
		$object->username= $_REQUEST['username'];
		$object->password=$_REQUEST['password'];
        $loginResult = AdminDAOImpl::getInstance()->loginUser($object);
        $loginjson=array("username"=>$loginResult->username,"result"=>$loginResult->result,"firstname"=>$loginResult->firstName,"admin_id"=>$loginResult->admin_id);
        $query = "SELECT `watch_id`,`vehicle_id`,`set_by`,`set_date_time`,
		`to_date`,`remind_note`,`first_name` FROM watchlist JOIN administrator on watchlist.set_by= administrator.admin_id ";
		$result = mysql_query($query) or die("Query ERROR".$query);
		$arrayCollection = new ArrayCollection();
        $reminderdata = array();
		$vresult = VehicleDAOImpl::getInstance()->getVehicleCount();      
        while ($row =  mysql_fetch_object($result))
        { 
			$querymessage = "SELECT count(is_read) AS 'read'  FROM message where vehicle_id= ".$row->vehicle_id." AND is_read=0"; 
			$resultmessage=mysql_query($querymessage) or die($querymessage);
			$resultmessagecount= mysql_fetch_object($resultmessage);	
			$queryimage = "SELECT `thump_url` FROM vehicle_image where vehicle_id= ".$row->vehicle_id; 
			$resultimage = mysql_query($queryimage) or die("Query ERROR");
			$image_path='';
			while ($row1 =  mysql_fetch_object($resultimage))
			{
				$image_path = $row1->thump_url;
				break;
			}
			$tmp[] = array(
			"watch_id" => $row->watch_id,
			"vehicle_id" => $row->vehicle_id,
			"set_by" => $row->set_by,
		    "set_date" => $row->set_date_time,
	        "to_date" => $row->to_date,
			"remind_note" => $row->remind_note,
			"remind_by" => $row->first_name,
			"image_path" => $image_path,
			"count_isread"=>$resultmessagecount->read
			);
        }
        $reminderdata = array("logdetails"=>$loginjson,"reminder"=>$tmp,"countnum"=>$vresult);
		echo json_encode($reminderdata);
	}
	if($action == 'login1')
	{ 	
		$object=new VehicleVO();
		$object->adminid= $_REQUEST['adminid'];
		$logquery ="SELECT * FROM `administrator` WHERE admin_id=".$object->adminid;
		$loginq = mysql_query($logquery) or die("Query ERROR".$logquery);
		$loginResult =  mysql_fetch_object($loginq);
        $loginjson=array("firstname"=>$loginResult->first_name,"email"=>$loginResult->email,"admin_id"=>$loginResult->admin_id,"admin_type"=>$loginResult->admin_type);
        $query = "SELECT `watch_id`,`vehicle_id`,`set_by`,`set_date_time`,
		`to_date`,`remind_note`,`first_name` FROM watchlist JOIN administrator on watchlist.set_by= administrator.admin_id ";
		$result = mysql_query($query) or die("Query ERROR".$query);
		$arrayCollection = new ArrayCollection();
        $reminderdata = array();
		$vresult = VehicleDAOImpl::getInstance()->getVehicleCount();      
        while ($row =  mysql_fetch_object($result))
        { 
			$querymessage = "SELECT count(is_read) AS 'read'  FROM message where vehicle_id= ".$row->vehicle_id." AND is_read=0"; 
			$resultmessage=mysql_query($querymessage) or die($querymessage);
			$resultmessagecount= mysql_fetch_object($resultmessage);	
			$queryimage = "SELECT `thump_url` FROM vehicle_image where vehicle_id= ".$row->vehicle_id; 
			$resultimage = mysql_query($queryimage) or die("Query ERROR");
			$image_path='';
			while ($row1 =  mysql_fetch_object($resultimage))
			{
				$image_path = $row1->thump_url;
				break;
			}
			$tmp[] = array(
			"watch_id" => $row->watch_id,
			"vehicle_id" => $row->vehicle_id,
		    "set_by" => $row->set_by,
		    "set_date" => $row->set_date_time,
	        "to_date" => $row->to_date,
			"remind_note" => $row->remind_note,
			"remind_by" => $row->first_name,
			"image_path" => $image_path,
			"count_isread"=>$resultmessagecount->read
			);
        }
        $reminderdata = array("logdetails"=>$loginjson,"reminder"=>$tmp,"countnum"=>$vresult);
       echo json_encode($reminderdata);
	}
	if($action=='getforum')
	{
		$adforums = array();
		$parentID=new ForumVO();
		$parentID->parent_id=$_POST['pid'];
		$parentID->start_index=$_POST['start_index'];
		$parentID->interval=$_POST['interval'];
		$result = AdminDAOImpl::getInstance()->getCommentListByParentID($parentID);
		$adminforums = array();
		$ar_forum = $result->reply_array->source;
		for($i=0; $i<count($ar_forum); $i++)
		{  
			$adminforums[] = array(
			"comment_id" => $ar_forum[$i]->comment_id,
			"parent_id" => $ar_forum[$i]->parent_id,
			"admin_id" => $ar_forum[$i]->admin_id,
			"admin_url" => $ar_forum[$i]->admin_url,
			"root_id" => $ar_forum[$i]->root_id,
			"child" => $ar_forum[$i]->child,
			"comment_by" => $ar_forum[$i]->comment_by,
			"comment" => $ar_forum[$i]->comment,
			"date" => $ar_forum[$i]->date,
			"update_date" => $ar_forum[$i]->update_date,
			"priority" => $ar_forum[$i]->priority
			
			);
        }					
		$adforum = array("adminforums"=>$adminforums,"totalcount"=>$result->total_sub);       
        echo json_encode($adforum);	
	}
	if($action=='getadminlist')
	{
		$result = AdminDAOImpl::getInstance()->getAllAdministrator();
		$adminsarray = array();	
		$ar_admin = $result->source;
		for($i=0; $i<count($ar_admin); $i++)
		{  
			$adminsarray[] = array(
			"image_thump" => $ar_admin[$i]->image_thump,
			"email" => $ar_admin[$i]->email,
			"password" => $ar_admin[$i]->password,
			"admin_type" => $ar_admin[$i]->admin_type,
			"first_name" => $ar_admin[$i]->first_name,
			"last_name" => $ar_admin[$i]->last_name,
			);
        }		
		$admins = array("adminsarray"=>$adminsarray);       
        echo json_encode($admins);		
	}
?>